package Controler;

import Module.User;
import Module.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-12-22
 * Time: 21:07
 * To BE A Better Person-2020-3-2
 */
@WebServlet("/logon")
public class LogonServlet extends HttpServlet {
    /**
     * 注册操作
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String name=req.getParameter("username");
        String passwd=req.getParameter("password");
        String id=req.getParameter("userid");
        //获取gitee地址
        String gitadd=req.getParameter("gitaddr");
        //判断当前的输入是否是正确的
        System.out.println(name+" "+passwd+" "+id+" "+gitadd);
        if(name == null || passwd == null || id == null || gitadd == null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请输入正确的信息");
            return;
        }else if(name.equals("") || passwd.equals("") || id.equals("") || gitadd.equals("")){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("请输入正确的信息");
            return;
        }
        //检查是由有重复的名字
        UserDao userDao=new UserDao();

        if (userDao.selectByname(name) != null){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("用户名已被注册");
            return;
        }
        //检查无误加入数据库，返回登录界面
        User user=new User();
        user.setUsername(name);
        user.setPassword(passwd);
        user.setUserid(Integer.parseInt(id));
        user.setUseraddre(gitadd);
        userDao.addUser(user);
        resp.setStatus(302);
        resp.setHeader("Location","login.html");

    }
}
